import pygame
import random


class MoneyData(object):
    def __init__(self, num, moneyInit):
        self.num = num
        self.moneyList = [moneyInit] * num

    def move(self, speed=10):
        for k in range(speed):
            for i in range(len(self.moneyList)):
                j = random.randint(0, self.num-1)
                self.moneyList[i] -= 1
                self.moneyList[j] += 1

    def quantification(self):
        moneyList = self.moneyList
        moneyList.sort()
        quant = moneyList[-1] if moneyList[-1] >= abs(moneyList[0]) else abs(moneyList[0])
        return list(map(lambda x: x * 200 / quant, moneyList))



class MoneyGame(object):
    def __init__(self, num=100, moneyInit=100, size=7, space=3):
        pygame.init()
        self.num = num
        self.size = size
        self.space = space
        self.screenWidth = num * (size + space) + 20
        self.screenHeight = 500
        self.screen = pygame.display.set_mode((self.screenWidth, self.screenHeight))
        pygame.display.set_caption("残酷的现实")
        self.money = MoneyData(self.num, moneyInit)
        self.update_GUI()
        self.clock = pygame.time.Clock()
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    exit()
            self.clock.tick(40)
            self.money.move()
            self.update_GUI()

    def update_GUI(self):
        self.screen.fill((255, 255, 255))
        pygame.draw.line(self.screen, (0, 0, 0), (10, 250), (10+(self.space+self.size)*self.num, 250))  #
        # moneyList.sort()
        data = self.money.quantification()

        for i in range(self.num):
            if data[i] >= 0:
                cell = pygame.Rect(10 + i * (self.space + self.size) + self.space, 250 - data[i], self.size, data[i])
            else:
                cell = pygame.Rect(10 + i * (self.space + self.size) + self.space, 250, self.size, -data[i])
            pygame.draw.rect(self.screen, (0, 0, 255) if data[i] > 0 else (255, 0, 0), cell)

        pygame.display.update()


if __name__ == "__main__":
    moneyGame = MoneyGame(5,100,10,5)
